package com.lpit.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lpit.mapper.ArticleMapper;
import com.lpit.mapper.CommentMapper;
import com.lpit.mapper.CommentReplyMapper;
import com.lpit.pojo.Article;
import com.lpit.pojo.Comment;
import com.lpit.pojo.CommentReply;
import com.lpit.result.ResponseResult;
import com.lpit.service.CommentManagementService;
import com.lpit.vo.CommentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CommentManagementServiceImpl extends ServiceImpl<ArticleMapper, Article> implements CommentManagementService {

    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private CommentReplyMapper commentReplyMapper;
    /**
     * 查询评论
     *
     * @return
     */
    @Override
    public ResponseResult get(Integer id,Integer page,Integer pageSize) {

        Page<CommentVo> commentVoPage = articleMapper.queryPageByUserIdAndComment(new Page<>(page, pageSize), id);

        return new ResponseResult(200,commentVoPage,"success");
    }

    /**
     * 删除评论
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteComment(Integer id) {

        int i = commentMapper.deleteById(id);

        return i==1 ? new ResponseResult(200,null,"success") : new ResponseResult(504,null,"error");
    }

    /**
     * 评论置顶
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult commentTop(Integer id) {

        Comment comment = commentMapper.selectById(id);
        comment.setTop(1);

        int i = commentMapper.updateById(comment);
        return i==1 ? new ResponseResult(200,null,"success") : new ResponseResult(504,null,"error");
    }

    /**
     * 回复评论
     *
     * @param commentReply
     * @return
     */
    @Override
    public ResponseResult addComment(CommentReply commentReply) {

        int insert = commentReplyMapper.insert(commentReply);

        return insert==1 ? new ResponseResult(200,null,"success") : new ResponseResult(504,null,"error");
    }
}
